package com.mframe.persist;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.mframe.exception.MarsRuntimeException;
import com.mframe.query.PageData;
import com.mframe.query.SqlBuilder;
import com.mframe.system.LoginInfo;
import com.mframe.system.SystemManager;

public class PublicBSImpl implements PublicBS {
	private PublicDAO publicDAO;
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	
	public Date getCurrentTime() {		
		return new Date();
	}

	
	public LoginInfo getLoginInfo() {
		return  SystemManager.getLoginInfo();
	}

	protected HibernateTemplate getHibernateTemplate() {
		return publicDAO.getHibernateTemplate();
	}

	protected JdbcTemplate getJdbcTemplate() {
		return publicDAO.getJdbcTemplate();
	}

	
	public <T> void saveObject(T entity) {
		validateAdd(entity);
		publicDAO.save(entity);
	}

	
	public <T> void updateObject(T entity) {
		validateModify(entity);
		publicDAO.update(entity);
	}

	
	public <T> void delete(T entity) {
		validateDelete(entity);
		publicDAO.delete(entity);
	}

	
	public <T> void deleteById(Serializable id, Class<T> clazz) {
		publicDAO.deleteById(id, clazz);
	}

	
	public int executeHql(String hql, Object[] args) {
		return publicDAO.executeHql(hql, args);
	}

	
	public <T> T loadById(Class<T> clazz, Serializable id) {		
		return publicDAO.loadById(clazz, id);
	}

	
	public <T> T getById(Class<T> clazz, Serializable id) {
		return publicDAO.getById(clazz, id);
	}

	
	public <T> T findById(Class<T> clazz, Serializable id) {
		return publicDAO.getById(clazz, id);
	}

	
	public <T> List<T> findByHql(String hql, Object[] args) {
		return publicDAO.findByHql(hql, args);
	}

	
	public <T> List<T> findByHql(String hql, Map<String, Object> args) {
		return publicDAO.findByHql(hql, args);
	}
	
	public <T> List<T> findByMap(Class<T> clazz, Map<String,Object> params) {
		StringBuffer hql = new StringBuffer("from "+clazz.getName()+" as obj ");
		if(params==null||params.isEmpty()){
			return this.findByHql(hql.toString(),new Object[]{});			
		}
		
		Object[] queryParams = new Object[params.size()];
		hql.append(" where ");
		int i=0;
		for(Entry<String, Object> param : params.entrySet()){
			String strParam = "obj."+param.getKey()+"= ? ";
			if(i==0){
				hql.append(strParam);
			}else{
				hql.append(" and "+strParam);
			}
			queryParams[i]=param.getValue();
			i++;
		}
		return this.findByHql(hql.toString(), queryParams);
	}
	
	public PageData findForPage(String hql, Map<String, Object> pmap,
			int topage, int pagesize) {
		return publicDAO.findForPage(hql, pmap, topage, pagesize);
	}

	
	public int findRecordCount(String hql, Object[] args) {
		return publicDAO.findRecordCount(hql,args);
	}

	
	public boolean findExists(String hql, Object[] args) {
		return publicDAO.findExists(hql,args);
	}

	
	public List<Map<String, Object>> callForList(String storeName,
			Object[] paramValues) {
		return publicDAO.callForList(storeName, paramValues);
	}

	
	public Object callForObj(String storeName, Object[] paramValues, int sqlType) {
		return publicDAO.callForObj(storeName, paramValues, sqlType);
	}

	
	public void callForObj(String storeName, Object[] paramValues) {
		publicDAO.callForObj(storeName, paramValues);
	}

	
	public Long getNextSequence(String seqName) {
		return publicDAO.getNextSequence(seqName);
	}

	
	public void executeSql(String sql) {
		publicDAO.executeSql(sql);
	}

	
	public Map<String, Object> queryForMap(String sql, Object[] args) {
		return publicDAO.queryForMap(sql, args);
	}

	
	public <T> List<T> queryForBeanList(Class<T> clazz, String sql,
			Object[] args) {
		return publicDAO.queryForBeanList(clazz,sql,args);
	}

	
	public <T> T queryForBean(Class<T> returnClazz, String sql,
			Object[] paramsValues) {
		return publicDAO.queryForBean(returnClazz, sql, paramsValues);
	}

	
	public <T> PageData queryForPageData(String sql, Class<T> clazz,
			int topage, int pagesize, Object[] paramsValues) {
		return publicDAO.queryForPageData(sql, clazz, topage, pagesize, paramsValues);
	}

	
	public Object queryForObject(String sql, Object[] paramsValues) {
		return publicDAO.queryForObject(sql, paramsValues);
	}

	
	public Integer queryForInt(String sql, Object[] paramsValues) {
		return publicDAO.queryForInt(sql, paramsValues);
	}

	
	public Long queryForLong(String sql, Object[] paramsValues) {
		return publicDAO.queryForLong(sql, paramsValues);
	}

	
	public <T> List<T> queryForList(String sql, Object[] args,
			Class<T> elementType) {
		return publicDAO.queryForList(sql, args,elementType);
	}

	
	public Object queryBySqlBuilder(SqlBuilder builder) {
		return  publicDAO.queryBySqlBuilder(builder);
	}

	
	public Object queryUnique(SqlBuilder builder) {
		return publicDAO.queryUnique(builder);
	}
	
	protected void validateAdd(Object obj) throws MarsRuntimeException{}
	protected void validateModify(Object obj) throws MarsRuntimeException{}
	protected void validateDelete(Object obj) throws MarsRuntimeException{}
	// //////////////////注入DAO///////////////
	public PublicDAO getPublicDAO() {
		return publicDAO;
	}

	public void setPublicDAO(PublicDAO publicDAO) {
		this.publicDAO = publicDAO;
	}

}
